#include <stdio.h>
#include <stdlib.h>

#include "exporter.h"

int saveGnuPlotFormat(TriangleList * tl, char * filename){
	int i;
	FILE * f = fopen(filename, "w");
	if(f == NULL){
		printf("Could not open file to write.\n");
		return -1;
	}

	for(i = 0; i < tl->length; i++){
		fprintf(f, "%g %g\n%g %g\n%g %g\n%g %g\n\n", 
			getTriangle(tl, i)->a->x,
			getTriangle(tl, i)->a->y,
			getTriangle(tl, i)->b->x,
			getTriangle(tl, i)->b->y,
			getTriangle(tl, i)->c->x,
			getTriangle(tl, i)->c->y,
			getTriangle(tl, i)->a->x,
			getTriangle(tl, i)->a->y
		);
	}

	fclose(f);
	return 0;
}

void savePoints(PointList * pl, char * fileName){
	int i;
	FILE * f1;
	f1 = fopen(fileName, "w");
	
	fprintf(f1, "%ld\n", pl->length);
	
	for(i = 0; i < pl->length; i++)
		fprintf(f1, "%g %g\n", pl->points[i]->x, pl->points[i]->y);
	fclose(f1);
}

void saveEdgeLists(EdgeList ** lists, int n, char * fileName){
	int i, index = 0;
	Edge * e;
	FILE * file = fopen(fileName, "w");

	for(i = 0; i < n; i++){
		e = lists[i]->edges;
		
		while(e != NULL){
			fprintf(file, "%g %g\n%g %g\n\n", e->a->x, e->a->y, e->b->x, e->b->y);
			e = e->next;
		}
	}

	fclose(file);
}
